<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" v-show="showSearch">
      <el-row :gutter="10">
        <el-col :span="6">
          <el-form-item label="小区" prop="communityId">
            <communitySelect v-model="queryParams.communityId" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fitmentList">
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column
        label="小区名称"
        align="center"
        prop="communityName"
        width="100"
      >
        <template #default="scope">
          {{ matchCommunity(scope.row.communityId).name }}
        </template>
      </el-table-column>
      <el-table-column
        label="申请人姓名"
        align="center"
        width="100"
        prop="realName"
      />
      <el-table-column
        label="申请人电话"
        align="center"
        width="120"
        prop="phone"
      />
      <el-table-column
        label="房屋编号"
        align="center"
        width="100"
        prop="roomNum"
      />
      <el-table-column label="图片" align="center" prop="images" width="100">
        <template #default="scope">
          <el-image
            preview-teleported
            style="width: 50px; height: 50px"
            :src="scope.row.images.split(',')[0]"
            :preview-src-list="scope.row.images.split(',')"
            fit="fill"
          />
          <div class="text-[#333] text-[12px]">
            共{{ scope.row.images.split(",").length }}张
          </div>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">{{
            getStatusText(scope.row.status)
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="施工开始时间"
        align="center"
        prop="constructionStartTime"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.constructionStartTime, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="预计施工天数"
        align="center"
        prop="constructionDays"
        width="120"
      />
      <el-table-column
        label="施工单位"
        align="center"
        prop="constructionUnit"
      />
      <el-table-column
        label="施工负责人电话"
        align="center"
        prop="leaderPhone"
        width="120"
      />

      <el-table-column label="操作" align="center" width="120" fixed="right">
        <template #default="scope">
          <el-button
            v-if="scope.row.status === 0"
            size="small"
            type="primary"
            link
            @click="auditHandle(scope.row)"
            >审核</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <review
      v-model="auditDialog"
      v-if="auditDialog"
      :row="curRow"
      @submit="getList"
    />
  </div>
</template>

<script setup>
import { ref, getCurrentInstance, reactive, onMounted } from "vue";
import communitySelect from "@/components/communitySelect";
import review from "./review.vue";
import { getFitmentPage } from "@/api/operateManage";
import { matchCommunity } from "@/common/match";

const { proxy } = getCurrentInstance();
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const fitmentList = ref([]);
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  communityName: undefined,
});

const curRow = ref({});
const auditDialog = ref(false);
onMounted(() => {
  getList();
});

/** 查询装修申请列表 */
function getList() {
  loading.value = true;
  getFitmentPage(queryParams).then(({ data }) => {
    fitmentList.value = data.records;
    total.value = data.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNo = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

function auditHandle(row) {
  curRow.value = row;
  auditDialog.value = true;
}

function getStatusType(status) {
  const statusMap = {
    0: "info",
    1: "success",
    2: "danger",
  };
  return statusMap[status] || "info";
}

// 获取状态文本
function getStatusText(status) {
  const statusMap = {
    0: "待审核",
    1: "已审核",
    2: "已拒绝",
  };
  return statusMap[status] || "未知状态";
}
</script>
<style scoped></style>
